#region MIT
/*
Blue.ResourceManager
Part of Engine Of Evermore (http://code.google.com/p/engineofevermore/)
Copyright (c) 2008, 2009 Michael Woerister

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
 */
#endregion

using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.IO;
using System.Diagnostics;
using NUnit.Framework;

namespace Blue.ResourceManager
{
	namespace Test
	{
		
		/// <summary>
		/// A simple Resource for testing with one constructor
		/// </summary>
		class TestResource : Resource
		{
			public TestResource( String name ) : base ( name ){}
		}
		
		/// <summary>
		/// A readonly FileSystemResourceManager to test the behaviour of non-writable objects
		/// </summary>
		class ReadonlyFSResourceManager : FileSystemResourceManager
		{
			public override bool IsReadOnly
			{
				get { return true; }
			}
			
			public ReadonlyFSResourceManager( String resourcePath ) : base( resourcePath )
			{
				
			}
		}
		
		[TestFixture]
		public class TestResourceManager
		{
			string path;
			DirectoryInfo di;
			
			[SetUp]
			public void Init()
			{
				path = @"testdir";
				
				try
				{
					if ( Directory.Exists( path ) )
					{
						Directory.Delete( path, true );
					}
					
					// Try to create the directory.
					di = Directory.CreateDirectory( path );					
				}
				catch ( Exception e )
				{
					Console.WriteLine( "The process failed: {0}", e.ToString() );
				}
			}
			
			[TearDown]
			public void Dispose()
			{
				if( !ResourceManager.IsDisposed )
				{
					ResourceManager.Dispose();
				}

				// Delete the directory
				di.Delete( true );
				
				if ( File.Exists( "testfile.test" ) )
				{
					File.Delete( "testfile.test" );	
				}
			}
			
			TestResource _TestDelegate( string str, Stream stream )
			{
				TestResource testresource = new TestResource( "test" );
				return testresource;
			}
			
			[Test]
			public void TestDispose()
			{
				FileSystemResourceManager fsrm = new FileSystemResourceManager( path );
				
				ResourceManager.Initialize( fsrm );
				ResourceManager.Dispose();
				
				Assert.That( ResourceManager.IsDisposed, Is.True );
			}
			
			[Test]
			public void TestRegisterResourceFactory()
			{
				FileSystemResourceManager fsrm = new FileSystemResourceManager( path );
				CreateResourceDelegate testdel = _TestDelegate;

				ResourceManager.Initialize( fsrm );
				
				string filepath = "testfile";
				FileStream fs = new FileStream( filepath, FileMode.Create );
				fs.Close();
				
				Resource tr = testdel( filepath, fs );
				
				ResourceManager.RegisterResourceFactory( ".test", testdel );
				
				Assert.That( ResourceManager.IsDisposed, Is.False );
				Assert.That( tr.ResourceName, Is.EqualTo( "test" ) );
				
				fs.Dispose();

				if ( File.Exists( filepath ) )
				{
					File.Delete( filepath );
				}
			}
			
			[Test]
			public void TestGetResourceHandle()
			{
				FileSystemResourceManager fsrm = new FileSystemResourceManager( path );
				CreateResourceDelegate testdel = _TestDelegate;
				
				ResourceManager.Initialize( fsrm );
				
				FileStream fs = File.Create( path + Path.DirectorySeparatorChar + "testfile.test" );
				fs.Close();
				
				ResourceManager.RegisterResourceFactory( ".test", testdel );
				
				TestResource tr = ResourceManager.GetResource<TestResource>( "testfile.test" );
				
				Assert.That( tr, Is.Not.Null );
			}
			
			[Test]
			public void TestIsResourceLoaded()
			{
				FileSystemResourceManager fsrm = new FileSystemResourceManager( path );
				CreateResourceDelegate testdel = _TestDelegate;
				
				ResourceManager.Initialize( fsrm );

				FileStream fs = File.Create( path + Path.DirectorySeparatorChar + "testfile.test" );
				fs.Close();
				
				ResourceManager.RegisterResourceFactory( ".test", testdel );

				ResourceManager.GetResource<TestResource>( "testfile.test" );
				
				bool isloaded = ResourceManager.IsResourceLoaded( "testfile.test" );
				Assert.That( isloaded, Is.True );
			}
			
			[Test]
			public void TestRenameResource()
			{
				FileSystemResourceManager fsrm = new FileSystemResourceManager( path );
				CreateResourceDelegate testdel = _TestDelegate;
				bool renameWasTriggered = false;
				fsrm.ResourceRenamed += delegate( object sender, ResourceRenamedEventArgs e ) { renameWasTriggered = true; };
				
				ResourceManager.Initialize( fsrm );
				
				FileStream fs = File.Create( path + Path.DirectorySeparatorChar + "testfile.test" );
				fs.Close();
				
				ResourceManager.RegisterResourceFactory( ".test", testdel );
				
				ResourceManager.RenameResource( "testfile.test", "renamed.test" );
				
				Assert.That( File.Exists( path + Path.DirectorySeparatorChar + "renamed.test" ), Is.True );
				Assert.That( renameWasTriggered, Is.True );
			}
			
			[Test]
			[ExpectedException( "Blue.ResourceManager.ResourceManagerException" )]
			public void TestRenameResourceFail()
			{
				ReadonlyFSResourceManager fsrm = new ReadonlyFSResourceManager( path );
				CreateResourceDelegate testdel = _TestDelegate;
				
				ResourceManager.Initialize( fsrm );

				FileStream fs = File.Create( path + Path.DirectorySeparatorChar + "testfile.test" );
				fs.Close();
				
				ResourceManager.RegisterResourceFactory( ".test", testdel );
				
				ResourceManager.RenameResource( ".test", ".new" );
			}
			
			[Test]
			public void TestOpenStream()
			{
				FileSystemResourceManager fsrm = new FileSystemResourceManager( path );
				CreateResourceDelegate testdel = _TestDelegate;
				
				ResourceManager.Initialize( fsrm );

				FileStream fs = File.Create( path + Path.DirectorySeparatorChar + "testfile.test" );
				fs.Close();
				
				ResourceManager.RegisterResourceFactory( ".test", testdel );
				
				string teststring = "teststring for testing reason";
				string resultstring = null;
				
				using( StreamWriter writer = new StreamWriter( ResourceManager.OpenStream( "testfile.test", ResourceOpenMode.Write ) ) )
				{
					writer.Write( teststring );
				}
				
				using( StreamReader reader = new StreamReader( path + Path.DirectorySeparatorChar + "testfile.test" ) )
				{
					resultstring = reader.ReadToEnd();
				}
				
				Assert.That( resultstring, Is.EqualTo( teststring ) );
			}
			
			[Test]
			[ExpectedException( "Blue.ResourceManager.ResourceManagerException" )]
			public void TestOpenStreamFail()
			{
				ReadonlyFSResourceManager fsrm = new ReadonlyFSResourceManager( path );
				CreateResourceDelegate testdel = _TestDelegate;
				
				ResourceManager.Initialize( fsrm );

				FileStream fs = File.Create( path + Path.DirectorySeparatorChar + "testfile.test" );
				fs.Close();
				
				ResourceManager.RegisterResourceFactory( ".test", testdel );
				
				byte[] arr = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
				using( FileStream writer = ( FileStream ) ResourceManager.OpenStream( "testfile.test", ResourceOpenMode.Write ) )
				{
					writer.Write( arr, 0, arr.Length );
				}
			}
			
			[Test]
			public void TestNormalizeName()
			{
				FileSystemResourceManager fsrm = new FileSystemResourceManager( path );
				string str1 = @" /dev\ null  ";
				string str2 = @"/dev /sda1";
				string str3 = @" \dev\hda2 ";
				
				ResourceManager.Initialize( fsrm );
				
				string result_str1 = ResourceManager.NormalizeName( str1 );
				string result_str2 = ResourceManager.NormalizeName( str2 );
				string result_str3 = ResourceManager.NormalizeName( str3 );

				Assert.That( result_str1, Is.EqualTo( @"dev/ null" ) );
				Assert.That( result_str2, Is.EqualTo( @"dev /sda1" ) );
				Assert.That( result_str3, Is.EqualTo( @"dev/hda2" ) );
			}
			
			[Test]
			public void TestFindResources()
			{
				FileSystemResourceManager fsrm = new FileSystemResourceManager( path );
				CreateResourceDelegate testdel = _TestDelegate;
				string anotherdir = path + Path.DirectorySeparatorChar + "anotherdir";
				string subdir = anotherdir + Path.DirectorySeparatorChar + "subdir";
				
				ResourceManager.Initialize( fsrm );
				
				// create subdirectories to testdir
				try
				{
					if ( Directory.Exists( anotherdir ) )
					{
						Directory.Delete( anotherdir, true );
					}

					Directory.CreateDirectory( anotherdir );
					
					if ( Directory.Exists( subdir ) )
					{
						Directory.Delete( subdir, true );
					}
					
					Directory.CreateDirectory( subdir );
					
				}
				catch ( Exception e )
				{
					Console.WriteLine( e );
				}

				File.Create( anotherdir + Path.DirectorySeparatorChar + "testfile.test" ).Close();
				File.Create( anotherdir + Path.DirectorySeparatorChar + "not_to_find.not" ).Close();
				File.Create( subdir + Path.DirectorySeparatorChar + "subfile.test").Close();
				
				ResourceManager.RegisterResourceFactory( ".test", testdel );
				
				List<String> resultlist = new List<String>();
				
				foreach( String str in ResourceManager.FindResources( "anotherdir", ".test" ) )
				{
					resultlist.Add(str);
				}
				
				List<String> shouldbe = new List<String>();
				shouldbe.Add( "testfile.test" );
				shouldbe.Add( "subdir/subfile.test" );
				
				Assert.That( resultlist, Is.EquivalentTo( shouldbe ) );
			}

            [Test]
            public void TestFindResourcesDocStringExample()
            {
                FileSystemResourceManager fsrm = new FileSystemResourceManager( path );
                ResourceManager.Initialize( fsrm );
                // /Images
                //     /x.png
                //     /y.png
                //     /z.bmp
                //     /abc
                //         /q.png

                Directory.CreateDirectory( path + Path.DirectorySeparatorChar + "Images" );
                Directory.CreateDirectory( path + Path.DirectorySeparatorChar + "Images" + 
                    Path.DirectorySeparatorChar + "abc" );

                File.Create( path + Path.DirectorySeparatorChar + "Images" + 
                    Path.DirectorySeparatorChar + "x.png" ).Close();

                File.Create( path + Path.DirectorySeparatorChar + "Images" +
                    Path.DirectorySeparatorChar + "y.png" ).Close();
                File.Create( path + Path.DirectorySeparatorChar + "Images" +
                    Path.DirectorySeparatorChar + "z.bmp" ).Close();
                File.Create( path + Path.DirectorySeparatorChar + "Images" +
                    Path.DirectorySeparatorChar + "abc" + Path.DirectorySeparatorChar + "q.png" )
                    .Close();
                
                var result1 = 
                    new List<String>( ResourceManager.FindResources( "Images", ".png" ) );

                var result2 = 
                    new List<String>( ResourceManager.FindResources( "/Images/", ".png" ) );

                var result3 = 
                    new List<String>( ResourceManager.FindResources( "Images/", ".png" ) );

                var result4 = 
                    new List<String>( ResourceManager.FindResources( "/Images", ".png" ) );

                var expected = new String[] { "x.png", "y.png", "abc/q.png" };

                Assert.That( result1, Is.EquivalentTo( expected ) );
                Assert.That( result2, Is.EquivalentTo( expected ) );
                Assert.That( result3, Is.EquivalentTo( expected ) );
                Assert.That( result4, Is.EquivalentTo( expected ) );
            }

            [Test]
            public void TestFindResourcesInRoot()
            {
                FileSystemResourceManager fsrm = new FileSystemResourceManager( path );
                
                ResourceManager.Initialize( fsrm );

                Directory.CreateDirectory( path + Path.DirectorySeparatorChar + "subdir" );

                File.Create( path + Path.DirectorySeparatorChar + "testfile1.test" ).Close();
                File.Create( path + Path.DirectorySeparatorChar + "subdir" + Path.DirectorySeparatorChar + "testfile2.test" ).Close();
                File.Create( path + Path.DirectorySeparatorChar + "testfile3.abc" ).Close();

                ResourceManager.RegisterResourceFactory( ".test", _TestDelegate );

                List<String> filesFoundWithRootSlash = 
                    new List<String>( ResourceManager.FindResources( "/", ".test" ) );

                List<String> filesFoundWithoutRootSlash =
                    new List<String>( ResourceManager.FindResources( "", ".test" ) );

                var expected = new String[] { "testfile1.test", "subdir/testfile2.test" };

                Assert.That( filesFoundWithRootSlash, Is.EquivalentTo( expected ) );
                Assert.That( filesFoundWithoutRootSlash, Is.EquivalentTo( expected ) );
            }

			[Test]
			public void TestFindDirectories()
			{
				FileSystemResourceManager fsrm = new FileSystemResourceManager( path );
				
				// we test with
				// testdir/anotherdir
				// testdir/anotherdir/subdir1
				// testdir/anotherdir/subdir2
				// testdir/anotherdir/subdir1/subsubdir1
				string anotherdir = path + Path.DirectorySeparatorChar + "anotherdir";
				string subdir1 = anotherdir + Path.DirectorySeparatorChar + "subdir1";
				string subdir2 = anotherdir + Path.DirectorySeparatorChar + "subdir2";
				string subsubdir1 = subdir1 + Path.DirectorySeparatorChar + "subsubdir1";
				
				ResourceManager.Initialize( fsrm );
				
				// create subdirectories to testdir
				try
				{
					if ( Directory.Exists( anotherdir ) )
					{
						Directory.Delete( anotherdir, true );
					}

					Directory.CreateDirectory( anotherdir );
					
					if ( Directory.Exists( subdir1 ) )
					{
						Directory.Delete( subdir1, true );
					}
					
					Directory.CreateDirectory( subdir1 );
					
					if ( Directory.Exists( subdir2 ) )
					{
						Directory.Delete( subdir2, true );
					}
					
					Directory.CreateDirectory( subdir2 );
					
					if ( Directory.Exists( subsubdir1 ) )
					{
						Directory.Delete( subsubdir1, true );
					}
					
					Directory.CreateDirectory( subsubdir1 );
					
				}
				catch ( Exception e )
				{
					Console.WriteLine( e );
				}
				
				System.Collections.Generic.List<String> resultlist = new System.Collections.Generic.List<String>();
				
				foreach( String str in ResourceManager.FindDirectories( "anotherdir" ) )
				{
					resultlist.Add(str);
				}
				
				System.Collections.Generic.List<String> shouldbe = new System.Collections.Generic.List<String>();
				shouldbe.Add( "subdir1" );
				shouldbe.Add( "subdir2" );
				shouldbe.Add( "subdir1/subsubdir1" );
				
				Assert.That( resultlist, Is.EquivalentTo( shouldbe ) );
			}
			
			[Test]
			public void TestResourceExists()
			{
				FileSystemResourceManager fsrm = new FileSystemResourceManager( path );
				CreateResourceDelegate testdel = _TestDelegate;
				
				ResourceManager.Initialize( fsrm );

				FileStream fs = File.Create( path + Path.DirectorySeparatorChar + "testfile.test" );
				fs.Close();
				
				ResourceManager.RegisterResourceFactory( ".test", testdel );
				
				ResourceManager.GetResource<TestResource>( "testfile.test" ); // not necessary
				
				Assert.That( ResourceManager.ResourceExists( "testfile.test" ), Is.True );
			}
			
			[Test]
			public void TestResourceDirectoryExists()
			{
				FileSystemResourceManager fsrm = new FileSystemResourceManager( path );
				string anotherdir = path + Path.DirectorySeparatorChar + "anotherdir";
				
				ResourceManager.Initialize( fsrm );
				
				try
				{
					if ( Directory.Exists( anotherdir ) )
					{
						Directory.Delete( anotherdir, true );
					}

					Directory.CreateDirectory( anotherdir );
				}
				catch ( Exception e )
				{
					Console.WriteLine( e );
				}
				
				Assert.That( ResourceManager.ResourceDirectoryExists( "anotherdir" ), Is.True );
				
			}
			
			[Test]
			public void TestCreateResource()
			{
				FileSystemResourceManager fsrm = new FileSystemResourceManager( path );
				ResourceManager.Initialize( fsrm );
				bool createWasTriggered = false;
				fsrm.ResourceCreated += delegate( object sender, ResourceCreatedEventArgs e ) { createWasTriggered = true; };
				
				ResourceCreationStream rcs = ResourceManager.CreateResource( "createdResource.test" );
				rcs.Close();
				
				Assert.That( createWasTriggered, Is.True );
				Assert.That( ResourceManager.ResourceExists( "createdResource.test" ), Is.True );
				Assert.That( File.Exists( path + Path.DirectorySeparatorChar + "createdResource.test" ), Is.True );
			}
			
			[Test]
			[ExpectedException( "Blue.ResourceManager.ResourceManagerException" )]
			public void TestCreateResourceFail()
			{
				ReadonlyFSResourceManager fsrm = new ReadonlyFSResourceManager( path );
				ResourceManager.Initialize( fsrm );
				
				ResourceManager.CreateResource( "createdResource.test" );
			}
			
			[Test]
			public void TestCreateResourceDirectory()
			{
				FileSystemResourceManager fsrm = new FileSystemResourceManager( path );
				ResourceManager.Initialize( fsrm );
				bool createWasTriggered = false;
				fsrm.ResourceDirectoryCreated += delegate( object sender, ResourceCreatedEventArgs e ) { createWasTriggered = true; };
				
				ResourceManager.CreateResourceDirectory( "subdir" );
				
				Assert.That( createWasTriggered, Is.True );
				Assert.That( ResourceManager.ResourceDirectoryExists( "subdir" ), Is.True );
				Assert.That( Directory.Exists( path + Path.DirectorySeparatorChar + "subdir" ) );
			}
			
			[Test]
			[ExpectedException( "Blue.ResourceManager.ResourceManagerException" )]
			public void TestCreateResourceDirectoryFail()
			{				
				ReadonlyFSResourceManager fsrm = new ReadonlyFSResourceManager( path );
				ResourceManager.Initialize( fsrm );
				
				ResourceManager.CreateResourceDirectory( "subdir" );
			}
			
			[Test]
			public void TestRenameResourceDirectory()
			{
				FileSystemResourceManager fsrm = new FileSystemResourceManager( path );
				ResourceManager.Initialize( fsrm );
				bool renameResourceWasTriggered = false;
				bool renameDirectoryWasTriggered = false;
				fsrm.ResourceDirectoryRenamed += delegate( object sender, ResourceRenamedEventArgs e ) { renameDirectoryWasTriggered = true; };
				fsrm.ResourceRenamed += delegate( object sender, ResourceRenamedEventArgs e ) { renameResourceWasTriggered = true; };
				
				ResourceManager.CreateResourceDirectory( "subdir" );
				ResourceCreationStream rcs = ResourceManager.CreateResource( "subdir" + Path.DirectorySeparatorChar + "resource.test" );
				rcs.Close();
				ResourceManager.CreateResourceDirectory( "subdir2" );
				
				ResourceManager.RenameResourceDirectory( "subdir", "renamedSubdir" );
				
				Assert.That( renameResourceWasTriggered, Is.True );
				Assert.That( renameDirectoryWasTriggered, Is.True );
				Assert.That( Directory.Exists( path + Path.DirectorySeparatorChar + "renamedSubdir" ), Is.True );
				Assert.That( Directory.Exists( path + Path.DirectorySeparatorChar + "subdir" ), Is.False );
				Assert.That( Directory.Exists( path + Path.DirectorySeparatorChar + "subdir2" ), Is.True );
			}
			
			[Test]
			[ExpectedException( "Blue.ResourceManager.ResourceManagerException" )]
			public void TestRenameResourceDirectoryFail()
			{
				ReadonlyFSResourceManager fsrm = new ReadonlyFSResourceManager( path );
				string anotherdir = path + Path.DirectorySeparatorChar + "anotherdir";
				
				ResourceManager.Initialize( fsrm );
				
				try
				{
					if ( Directory.Exists( anotherdir ) )
					{
						Directory.Delete( anotherdir, true );
					}

					Directory.CreateDirectory( anotherdir );
				}
				catch ( Exception e )
				{
					Console.WriteLine( e );
				}
				
				ResourceManager.RenameResourceDirectory( "anotherdir", "renamedDir" );
			}
			
			[Test]
			public void TestDeleteResource()
			{
				FileSystemResourceManager fsrm = new FileSystemResourceManager( path );
				ResourceManager.Initialize( fsrm );
				bool deleteWasTriggered = false;
				fsrm.ResourceDeleted += delegate(object sender, ResourceDeletedEventArgs e) { deleteWasTriggered = true; };
				
				ResourceCreationStream rcs = ResourceManager.CreateResource( "createdResource.test" );
				rcs.Close();
				
				ResourceManager.DeleteResource( "createdResource.test" );
				
				Assert.That( deleteWasTriggered, Is.True );
				Assert.That( ResourceManager.ResourceExists( "createdResource.test" ), Is.False );
				Assert.That( File.Exists( path + Path.DirectorySeparatorChar + "createdResource.test" ), Is.False );
			}
			
			[Test]
			[ExpectedException( "Blue.ResourceManager.ResourceManagerException" )]
			public void TestDeleteResourceFail()
			{
				ReadonlyFSResourceManager fsrm = new ReadonlyFSResourceManager( path );
				ResourceManager.Initialize( fsrm );
								
				FileStream fs = new FileStream( "testfile.test", FileMode.Create );
				fs.Close();
				
				ResourceManager.DeleteResource( "testfile.test" );
			}
			
			[Test]
			public void TestDeleteResourceDirectory()
			{
				FileSystemResourceManager fsrm = new FileSystemResourceManager( path );
				ResourceManager.Initialize( fsrm );
				bool deleteResourceWasTriggered = false;
				bool deleteDirectoryWasTriggered = false;
				fsrm.ResourceDirectoryDeleted += delegate( object sender, ResourceDeletedEventArgs e ) { deleteDirectoryWasTriggered = true; };
				fsrm.ResourceDeleted += delegate( object sender, ResourceDeletedEventArgs e ) { deleteResourceWasTriggered = true; };
				
				ResourceManager.CreateResourceDirectory( "subdir" );
				ResourceCreationStream rcs = ResourceManager.CreateResource( "subdir" + Path.DirectorySeparatorChar + "resource.test" );
				rcs.Close();
				ResourceManager.CreateResourceDirectory( "subdir2" );
				
				ResourceManager.DeleteResourceDirectory( "subdir" );
				
				Assert.That( deleteResourceWasTriggered, Is.True );
				Assert.That( deleteDirectoryWasTriggered, Is.True );
				Assert.That( Directory.Exists( path + Path.DirectorySeparatorChar + "subdir" ), Is.False );
				Assert.That( Directory.Exists( path + Path.DirectorySeparatorChar + "subdir2" ), Is.True );
			}
			
			[Test]
			[ExpectedException( "Blue.ResourceManager.ResourceManagerException" )]
			public void TestDeleteResourceDirectoryFail()
			{
				ReadonlyFSResourceManager fsrm = new ReadonlyFSResourceManager( path );
				string anotherdir = path + Path.DirectorySeparatorChar + "anotherdir";
				
				ResourceManager.Initialize( fsrm );
				
				try
				{
					if ( Directory.Exists( anotherdir ) )
					{
						Directory.Delete( anotherdir, true );
					}

					Directory.CreateDirectory( anotherdir );
				}
				catch ( Exception e )
				{
					Console.WriteLine( e );
				}
				
				ResourceManager.DeleteResourceDirectory( "anotherdir");
			}
			
			[Test]
			public void TestIsResourceDirectoryEmpty()
			{
				FileSystemResourceManager fsrm = new FileSystemResourceManager( path );
				ResourceManager.Initialize( fsrm );
								
				ResourceManager.CreateResourceDirectory( "subdir" );
				ResourceCreationStream rcs = ResourceManager.CreateResource( "subdir" + Path.DirectorySeparatorChar + "resource.test" );
				rcs.Close();
				ResourceManager.CreateResourceDirectory( "subdir2" );
				
				Assert.That( ResourceManager.IsResourceDirectoryEmpty( "subdir" ), Is.False );
				Assert.That( ResourceManager.IsResourceDirectoryEmpty( "subdir2" ), Is.True );
			}
			
			[Test]
			public void TestIsValidResourceName()
			{
				FileSystemResourceManager fsrm = new FileSystemResourceManager( path );
				
				ResourceManager.Initialize( fsrm );
				
				Assert.That( ResourceManager.IsValidResourceName( "" ), Is.False );
				Assert.That( ResourceManager.IsValidResourceName( "invalid<" ), Is.False );
				Assert.That( ResourceManager.IsValidResourceName( "invalid//" ), Is.False );
				Assert.That( ResourceManager.IsValidResourceName( "invalid." ), Is.False );
				Assert.That( ResourceManager.IsValidResourceName( "valid" + Path.DirectorySeparatorChar + "resource.test" ), Is.True );
			}
		}
	}
}